package com.vinjogames.entities;

import javax.microedition.khronos.opengles.GL10;

import com.vinjogames.gfx.Image;
import com.vinjogames.util.GameConVars;
import com.vinjogames.util.Preferences;

//TODO: Comment this class.
public class Base extends GameObject {
	private int cannonslots;
	private int unitBuildRemaining;
	private Turret[] cannons;
	private Player myPlayer;
	private boolean resourceUpgrade;
	private boolean turretUpgrade;
	private boolean productionUpgrade;
	private boolean healthUpgrade;
	
	public Base(float x, float y, Player p) {
		super(GameConVars.OBJECT_BASE);
		mLocation.set(x,y);
		mGLWidth = 4;
		mGLHeight = 1;
		myPlayer = p;
		
        mImage = new Image(GameConVars.GRAPHIC_BASES[0], mGLWidth, mGLHeight);
        health = maxHealth = GameConVars.BASE_DEFAULT_HEALTH;
	}
	
	public void setPlayer(Player p) { myPlayer = p; }
	public Player getPlayer() { return this.myPlayer; }
		
	public void clearInfo() {}
	
	public boolean purchaseUpgrade(int purchaseNum) {
		switch(purchaseNum) {
		case GameConVars.BASE_HEALTH_PURCHASE:
			if(myPlayer.resourcesRemaining < GameConVars.BUY_BASE_HEALTH_INCREASE) return false;
			return upgradeHealth();
		case GameConVars.BASE_RESOURCE_PURCHASE:
			if(myPlayer.resourcesRemaining < GameConVars.BUY_BASE_INCREASE_RESOURCE) return false;
			return upgradeResources();
		case GameConVars.BASE_QUARTER_HEAL_PURCHASE:
			if(myPlayer.resourcesRemaining < GameConVars.BUY_BASE_QUARTER_HEAL) return false;
			return quarterHeal();
		case GameConVars.BASE_HALF_HEAL_PURCHASE:
			if(myPlayer.resourcesRemaining < GameConVars.BUY_BASE_HALF_HEAL) return false;
			return halfHeal();
		case GameConVars.BASE_DOUBLE_UNIT_PURCHASE:
			if(myPlayer.resourcesRemaining < GameConVars.BUY_BASE_DOUBLE_UNIT_PRODUCTION) return false;
			return upgradeProduction();
		case GameConVars.BASE_TURRET_PURCHASE:
			if(myPlayer.resourcesRemaining < GameConVars.BUY_BASE_TURRET_SLOT) return false;
			return upgradeTurrets();
		default:
			return false;
		}
	}
	
	private boolean upgradeResources() {
		if(resourceUpgrade) return false;
		myPlayer.increaseResourcesPerTurn(GameConVars.BASE_BONUS_RESOURCES);
		return resourceUpgrade = true;
	}	
	private boolean upgradeTurrets() { return (!turretUpgrade ? turretUpgrade = true : false); }
	private boolean upgradeProduction() { return (!productionUpgrade ? productionUpgrade = true : false); }
	private boolean upgradeHealth() {
		if(healthUpgrade) return false;
		health += GameConVars.BASE_BONUS_HEALTH;
		maxHealth += GameConVars.BASE_BONUS_HEALTH;
		return healthUpgrade = true;
	}
	private boolean quarterHeal() {
		health = (health + (maxHealth / 4)) > maxHealth ? health + (maxHealth / 4) : maxHealth; 
		return true;
	}
	private boolean halfHeal() {
		health = (health + (maxHealth / 2)) > maxHealth ? health + (maxHealth / 2) : maxHealth; 
		return true;
	}
	
	public void draw(GL10 gl){
        gl.glPushMatrix();
        gl.glTranslatef(0, mGLHeight * 0.5f, 0);
		super.draw(gl);
		gl.glPopMatrix();
		drawBars(gl, mLocation.x, mLocation.y + mGLHeight/2, mGLWidth, mGLHeight, health/maxHealth, 0,1,0,1);
		if(Preferences.isDebug())drawCollisionBox(gl);
	}
}
